package com.auoem.radio.ui.other;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;

import com.auctus.am.framework.AmContext;
import com.auctus.am.framework.constants.AmConstants;
import com.auctus.am.framework.imp.RadioManager;
import com.auctus.am.framework.interfaces.ComCallback;
import com.auctus.am.framework.roomdb.repository.AmMsgRep;
import com.auctus.am.framework.tasks.RadioTaskManager;
import com.auoem.radio.MainActivity;
import com.auoem.radio.base.BaseActivity;
import com.auoem.radio.base.OemApplication;
import com.auoem.radio.databinding.ActivityFlashBinding;

/**
 * 闪屏页
 */
public class FlashActivity extends BaseActivity implements View.OnClickListener {

    ActivityFlashBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mBinding = ActivityFlashBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        initAm();
    }

    /*
    * 初始化SDK模块
    * */
    private void initAm() {
        if (AmConstants.IS_DEBUG_BEFORE_DEV) {
            AmMsgRep.getInstance().init(AmContext.getInstance().getAppContext());
            RadioManager.getInstance().init();
            goTestTalk();
        } else {
//            AmMsgRep.getInstance().init(AmContext.getInstance().getAppContext());
            new Thread(() -> {
                /*Step-2 初始化A6 */
                OemApplication.getApplication().initAm();
                /*Step-3 启动基础模块*/
                startRadioTask();
            }).start();
        }
    }

    private void startRadioTask() {
        /*Step-3.1 通过任务管理器进入系统*/
        RadioTaskManager.getInstance().startEnterTask(RadioManager.getInstance().getRadioData(), new ComCallback<Boolean>() {
            @Override
            public void onCallback(Boolean aBoolean, int code, String inf) {
                goHome();
            }
        });
    }

    private void initActionBar() {
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.hide();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    private void goHome() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void goTestTalk() {
        Intent intent = new Intent(this, TestDbActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onClick(View v) {
        if (v == mBinding.tvInit) {
//            initAm();
        }
    }
}
